var express = require('express');
let db = require('./link.js')
var app = express();
// var bodyParser = require('body-parser')

app.use(express.json())
// 表单请求
app.use(express.urlencoded({ extended: false }))
//设置跨域访问
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "*");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1');
    // "text/html;charset=utf-8"
    res.header("Content-Type", 'application/x-www-form-urlencoded');
    next();
});
//封装函数---切割数组里面对象的某一相同属性值进行切割
function cutarr(Arr) {
    // let Pro="pro";
    let newArr = Arr;
    let nullArr = [];
    for (let [index, elem] of newArr.entries()) {
        let firstvalue = newArr[0].articleid;
        let value = elem.articleid;
        for (let i = 0; i < newArr.length + 1; i++) {
            if (firstvalue !== value) {
                let qqq = newArr.slice(0, index);
                nullArr.push(qqq);
                newArr.splice(0, index);
            }
        }
    }
    return nullArr
}
// console.log(typeof(db))

//归档Archive页面

//请求文章数据库内容
app.post('/Archive', async (req, resp) => {

    let obj = req.body;
    var strsql = "select * from article"
    let dbdata = await db(strsql)
    if (dbdata !== "err") {
        resp.status(200)
        resp.json(dbdata)
    } else {
        resp.status(404),
            resp.json("请求失败")
    }

});
//请求文章评论数据库
app.post('/ArchiveRev', async (req, resp) => {
    let obj = req.body;
    // select * from artreview order by articleid asc;
    var strsql = "select * from artreview order by articleid desc"
    // var strsql="select * from artreview"
    let dbdata = await db(strsql)
    if (dbdata !== "err") {
        resp.status(200)
        resp.json(dbdata)
    } else {
        resp.status(404),
            resp.json("请求失败")
    }

});
//写入文章博客评论数据库接口
app.post('/artReview', async (req, resp) => {
    let obj = req.body;
    // console.log(req.body)
    var strsql = `insert into artreview (articleid,autor,content,time) values('${obj.articleid}','${obj.revName}','${obj.revCon}','${obj.time}')`
    // console.log(strsql)
    let dbdata = await db(strsql)
    // console.log(dbdata)

    if (dbdata !== "err") {
        resp.status(200)
        resp.json("发布成功")
    } else {
        resp.status(404),
            resp.json("发布失败")
    }

});
//写入文章点赞数据库接口
app.post('/articleLike', async (req, resp) => {
    let obj = req.body;
    strsql = `insert into articlelike (articleid,uuid,ifClick) values('${obj.idNum}','${obj.useruuid}','${obj.ifClick}')`
    db(strsql).then((data) => {
        resp.status(200)
        resp.json(data)
    })
    .catch((err) => {
        let str = err
        let str1 = "Duplicate entry"
        let sql=""
        if (str.indexOf(str1) != -1) {
            sql = `update articlelike set  ifclick ='${obj.ifClick}'  where uuid='${obj.useruuid}' and articleid='${obj.idNum}' `
        } 
        return db(sql)
    }).then((res)=>{
        resp.status(200)
        resp.json(res)

    })

});
//请求文章点赞数据库
app.post('/artLikeNum', async (req, resp) => {
    let obj = req.body;
    var strsql = `select * from articlelike where ifClick = '${obj.value}'`
    let dbdata = await db(strsql)
    if (dbdata !== "err") {
        resp.status(200)
        resp.json(dbdata)
    } else {
        resp.status(404),
            resp.json("请求失败")
    }

});



//首页FrontPage页面

//请求首页说说数据库内容
app.post('/FrontPage', async (req, resp) => {
    var strsql = "select * from feeling where deltalk = 'true'"
    let dbdata = await db(strsql)
    // console.log(dbdata)
    let newData = dbdata.map(e => {
        e.img = e.img === '' ? [] : e.img.split(",")
        return e
    })
    if (dbdata !== "err") {
        resp.status(200)
        resp.json(newData)
    } else {
        resp.status(404),
            resp.json("请求失败")
    }
});
//请求首页说说评论数据库
app.post('/FrontPageRev', async (req, resp) => {
    let obj = req.body;
    var strsql = "";
    if (obj.value == "true") {
        var strsql = "select * from feelreview order by id desc"
    } else {
        var strsql = "select * from feelreview order by feelingid desc"
    }
    let dbdata = await db(strsql)
    // console.log(dbdata)
    if (dbdata !== "err") {
        resp.status(200)
        resp.json(dbdata)
    } else {
        resp.status(404),
            resp.json(err)
    }
});
//写入说说博客评论数据库接口
app.post('/feelReview', async (req, resp) => {
    let obj = req.body;
    // console.log(req.body)
    var strsql = `insert into feelreview (feelingid,autor,content,time) values('${obj.feelingid}','${obj.feelName}','${obj.feelCon}','${obj.time}')`
    // console.log(strsql)
    let dbdata = await db(strsql)
    // console.log(dbdata)
    if (dbdata !== "err") {
        resp.status(200)
        resp.json("发布成功")
    } else {
        resp.status(404),
            resp.json("发布失败")
    }

});
//写入说说点赞数据库接口
app.post('/feelLike', async (req, resp) => {
    let obj = req.body;
    strsql = `insert into feelinglike (feelingid,uuid,ifClick) values('${obj.idNum}','${obj.useruuid}','${obj.ifClick}')`
    db(strsql).then((data) => {
        resp.status(200)
        resp.json(data)
    })
    .catch((err) => {
        let str = err
        let str1 = "Duplicate entry"
        let sql=""
        if (str.indexOf(str1) != -1) {
            sql = `update feelinglike set  ifclick ='${obj.ifClick}'  where uuid='${obj.useruuid}' and feelingid='${obj.idNum}' `
        } 
        return db(sql)
    }).then((res)=>{
        resp.status(200)
        resp.json(res)

    })

});
//请求说说点赞数据库
app.post('/feelLikeNum', async (req, resp) => {
    let obj = req.body;
    // console.log(obj)
    // var strsql =`select * from feelinglike where ifClick = 'true' and feelingid = '${obj.value}' `
    var strsql = `select * from feelinglike where ifClick = '${obj.value}'`
    let dbdata = await db(strsql)
    // console.log(dbdata)
    if (dbdata !== "err") {
        resp.status(200)
        resp.json(dbdata)
    } else {
        resp.status(404),
            resp.json("发布失败")
    }

});


//发表write页面
//写入文章博客数据库接口
app.post('/article', async (req, resp) => {

    let obj = req.body;
    console.log(req.body)
    var strsql = `insert into article (title,content,time,autor) values('${obj.inputData}','${obj.textData}','${obj.time}','${obj.autorName}')`
    // console.log(strsql)
    let dbdata = await db(strsql)
    // console.log(dbdata)

    if (dbdata !== "err") {
        resp.status(200)
        resp.json("发布成功")
    } else {
        resp.status(404),
            resp.json("发布失败")
    }

});
//写入说说博客数据库接口
app.post('/feeling', async (req, resp) => {

    let obj = req.body;
    console.log(obj)
    var strsql = `insert into feeling (content,time,img,autor,headimg,deltalk) values('${obj.textdata}','${obj.time}','${obj.imageUrl}','${obj.autorName}','${obj.headimg}','true')`
    // console.log(strsql)
    let dbdata = await db(strsql)
    // console.log(dbdata)

    if (dbdata !== "err") {
        resp.status(200)
        resp.json("发布成功")
    } else {
        resp.status(404),
            resp.json("发布失败")
    }

});
//删除说说博客数据库接口
app.post('/feelingdel',async(req,resp)=>{
    let obj =req.body;
    console.log(obj)
    var strsql = `update feeling set  deltalk ='false'  where id='${obj.delnum}'`
    let dbdata = await db(strsql)

    if (dbdata !== "err") {
        resp.status(200)
        resp.json("修改成功")
    } else {
        resp.status(404),
            resp.json("修改失败")
    }
    
})


//配置服务端口
var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log('Example app listening at http://%s:%s', host, port);
})